

import java.io.*;
import java.net.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;






import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;

public class COM_short implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
		// System.out.println(extra[0]);
		// System.out.println(extra[1]);

		String start = "";
		if (usename == true) {
			start = sender + ": ";
		}

		if (extra.length != 0) {
			if (extra[0] != " ") {

				String tosend = null;
				String setting = "googl";
				Boolean send = true;
				if (extra[0].equalsIgnoreCase("googl")
						|| extra[0].equalsIgnoreCase("google")
						|| extra[0].equalsIgnoreCase("goo.gle")
						|| extra[0].equalsIgnoreCase("goo.gl")) {
					String add = null;
					if (!extra[1].contains("http://")) {
						add = "http://" + extra[1];
					} else {
						add = extra[1];

					}
					// stays the same, improve
					try {
						// Create a URL for the desired page
						URL url = new URL(
								"http://shortify.appspot.com/shorty?url="
										+ java.net.URLEncoder.encode(add,
												"UTF-8"));

						BufferedReader in = new BufferedReader(
								new InputStreamReader(url.openStream()));
						String str;
						String shortit = null;
						str = in.readLine();
						System.out.println((int) str.trim().charAt(0));

					//	System.out.println(str);
					    Pattern datePattern = Pattern.compile("\"short_url\":\"([^\"]+)\"");
					    Matcher dateMatcher = datePattern.matcher(str.trim());
					    if (dateMatcher.find())
					    {
					    	shortit = dateMatcher.group(1);
					    }

						tosend = "Your link has been shortened with goo.gl: "
								+ shortit;
						in.close();

					} catch (MalformedURLException e) {
						tosend = "Malformed URL!";
					} catch (IOException e) {
						tosend = "IOException!";
					}
				} else if (extra[0].equalsIgnoreCase("bit.ly")
						|| extra[0].equalsIgnoreCase("bitly")) {
					String add = null;
					if (!extra[1].contains("http://")) {
						add = "http://" + extra[1];
					} else {
						add = extra[1];
					}
					setting = "bitly";
					try {
						// Create a URL for the desired page
						URL url = new URL(
								"http://api.bitly.com/v3/shorten?login=hogofwar&apiKey=R_40f5820592679d0f96533c65ff8a381a&longUrl="
										+ java.net.URLEncoder.encode(add,
												"UTF-8") + "&format=txt");

						BufferedReader in = new BufferedReader(
								new InputStreamReader(url.openStream()));
						String str;
						while ((str = in.readLine()) != null) {
							tosend = tosend = "Your link has been shortened with bit.ly: "
									+ str;
						}
						in.close();

					} catch (MalformedURLException e) {
						tosend = "Malformed URL!";
					} catch (IOException e) {
						tosend = "IOException!";
					}
				} else if (extra[0].equalsIgnoreCase("jmp")
						|| extra[0].equalsIgnoreCase("j.mp")) {
					String add = null;
					if (!extra[1].contains("http://")) {
						add = "http://" + extra[1];
					} else {
						add = extra[1];
					}
					setting = "bitly";
					try {
						// Create a URL for the desired page
						URL url = new URL(
								"http://api.bitly.com/v3/shorten?login=hogofwar&domain=j.mp&apiKey=R_40f5820592679d0f96533c65ff8a381a&longUrl="
										+ java.net.URLEncoder.encode(add,
												"UTF-8") + "&format=txt");

						BufferedReader in = new BufferedReader(
								new InputStreamReader(url.openStream()));
						String str;
						while ((str = in.readLine()) != null) {
							tosend = tosend = "Your link has been shortened with j.mp: "
									+ str;
						}
						in.close();

					} catch (MalformedURLException e) {
						tosend = "Malformed URL!";
					} catch (IOException e) {
						tosend = "IOException!";
					}
				} else if (extra[0].equalsIgnoreCase("isgd")
						|| extra[0].equalsIgnoreCase("is.gd")) {
					String add = null;
					if (!extra[1].contains("http://")) {
						add = "http://" + extra[1];
					} else {
						add = extra[1];
					}
					setting = "bitly";
					try {
						// Create a URL for the desired page
						URL url = new URL(
								"http://is.gd/create.php?format=simple&url="
										+ java.net.URLEncoder.encode(add,
												"UTF-8"));

						BufferedReader in = new BufferedReader(
								new InputStreamReader(url.openStream()));
						String str;
						while ((str = in.readLine()) != null) {
							tosend = tosend = "Your link has been shortened with is.gd: "
									+ str;
						}
						in.close();

					} catch (MalformedURLException e) {
						tosend = "Malformed URL!";
					} catch (IOException e) {
						tosend = "IOException!";
					}
				} else if (extra[0].equalsIgnoreCase("tiny")
						|| extra[0].equalsIgnoreCase("tinyurl")) {
					String add = null;
					if (!extra[1].contains("http://")) {
						add = "http://" + extra[1];
					} else {
						add = extra[1];
					}
					setting = "bitly";
					try {
						// Create a URL for the desired page
						URL url = new URL(
								"http://tinyurl.com/api-create.php?url="
										+ java.net.URLEncoder.encode(add,
												"UTF-8"));

						BufferedReader in = new BufferedReader(
								new InputStreamReader(url.openStream()));
						String str;
						while ((str = in.readLine()) != null) {
							tosend = tosend = "Your link has been shortened with TinyURL: "
									+ str;
						}
						in.close();

					} catch (MalformedURLException e) {
						tosend = "Malformed URL!";
					} catch (IOException e) {
						tosend = "IOException!";
					}
				} else if (extra[0].equalsIgnoreCase("adfly")
						|| extra[0].equalsIgnoreCase("adf.ly")) {
					String add = null;
					if (!extra[1].contains("http")) {
						add = "http://" + extra[1];
					} else {
						add = extra[1];
					}
					System.out.println(add);
					setting = "bitly";
					try {
						// Create a URL for the desired page
						URL url = new URL(
								"http://api.adf.ly/api.php?key=4f452f287af9de41ba1d98618608b04f&uid=418801&advert_type=int&domain=adf.ly&url="
										+ java.net.URLEncoder.encode(add,
												"UTF-8"));

						BufferedReader in = new BufferedReader(
								new InputStreamReader(url.openStream()));
						String str;
						while ((str = in.readLine()) != null) {
							if (!str.equalsIgnoreCase("error")) {
								tosend = tosend = "Your link has been shortened with adf.ly: "
										+ str;
							} else {
								tosend = tosend = "There has been an error with your link.";
							}
						}
						in.close();

					} catch (MalformedURLException e) {
						tosend = "Malformed URL!";
					} catch (IOException e) {
						tosend = "IOException!";
					}
				} else if (extra[0].equalsIgnoreCase("help")
						|| extra[0].equalsIgnoreCase("list")) {
					irc.sendNotice(sender,
							"-- List of Available URL Shortners Commencing --");
					irc.sendNotice(sender,
							"Goo.gl - Shortens with http://goo.gl (default)");
					irc.sendNotice(sender,
							"Bit.ly - Shortens with http://bit.ly");
					irc.sendNotice(sender, "J.mp - Shortens with http://j.mp");
					irc.sendNotice(sender, "Is.gd - Shortens with http://is.gd");
					irc.sendNotice(sender,
							"Adf.ly - Shortens with http://adf.ly");
					irc.sendNotice(sender,
							"TinyURL - Shortens with http://tinyurl.com");
					irc.sendNotice(sender,
							"-- End of List of Available URL Shortners. --");
					send = false;
					tosend = "";
				} else {
					try {
						String add = null;
						if (!extra[0].contains("http://")) {
							add = "http://" + extra[0];
						} else {
							add = extra[0];
						}
						// Create a URL for the desired page
						URL url = new URL(
								"http://shortify.appspot.com/shorty?url="
										+ java.net.URLEncoder.encode(add,
												"UTF-8"));

						BufferedReader in = new BufferedReader(
								new InputStreamReader(url.openStream()));
						String str;
						String shortit = null;
						str = in.readLine();
						//System.out.println((int) str.trim().charAt(0));

						System.out.println(str);
						//Object obj=JSONValue.parse(str.trim());
						
						    Pattern datePattern = Pattern.compile("\"short_url\":\"([^\"]+)\"");
						    Matcher dateMatcher = datePattern.matcher(str.trim());
						    if (dateMatcher.find())
						    {
						    	shortit = dateMatcher.group(1);
						    }
						
						

						tosend = "Your link has been shortened: " + shortit;
						in.close();

					} catch (MalformedURLException e) {
						tosend = "Malformed URL!";
					} catch (IOException e) {
						tosend = "IOException!";
					}

				} // address.getCanonicalHostName()
				// tosend = " seems to be up.";

				if (send) {
			
					irc.sendMessage(channel, start + "" + tosend);
				}
			} else {
				
				irc.sendMessage(channel, start
						+ "You need to specify an input!");
			}
		} else {
			
			irc.sendMessage(channel, start + "You need to specify an input!");
		}
	}

	public String help() {
		return "Short - short (service) [website] - Shortens an URL. Use "
				+ IRCBase.prefix
				+ "short list to list all available shorteners.";
		// TODO Auto-generated method stub

	}

	// public static boolean exists(String URLName){
	// try {
	// HttpURLConnection.setFollowRedirects(true);
	// // note : you may also need
	// //HttpURLConnection.setInstanceFollowRedirects(false)
	// HttpURLConnection con =
	// (HttpURLConnection) new URL(URLName).openConnection();
	// con.setRequestMethod("HEAD");
	// return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
	// }
	// catch (Exception e) {
	// e.printStackTrace();
	// return false;
	// }
	// }
	public String version() {
		return "Short version 1";
		// TODO Auto-generated method stub

	}

	@Override
	public String restrict() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean hide() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String aliases() {
		// TODO Auto-generated method stub
		return "s, shorten, smallify, minify";
	}

}
